/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sabatec.controllers;

import br.com.sabatec.entities.UserSabatec;
import br.com.sabatec.persistence.JpaUtil;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import org.primefaces.context.RequestContext;
import sun.misc.BASE64Encoder;

/**
 *
 * @author Daniel.Morais
 */
@ManagedBean(name = "userSabatecBean")
public class UserSabatecBean {

    private UserSabatec user = new UserSabatec();
    private List<UserSabatec> users;
    public String msg = null;

    public UserSabatec getUsersabatec() {
        return user;
    }

    public void setUsersabatec(UserSabatec user) {
        this.user = user;
    }

    public void salva(UserSabatec user) {
        user.setSenha(encripta(user.getUser()));
        EntityManager em = JpaUtil.getEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        em.persist(user);
        tx.commit();
        em.close();
    }

    public List<UserSabatec> getUsersSabatec() {
        EntityManager em = JpaUtil.getEntityManager();
        Query q = em.createNamedQuery("UserSabatec.findAll");
        this.users = q.getResultList();
        em.close();
        return users;

    }

    public String getUserSabatecPass(UserSabatec user) {
        EntityManager em = JpaUtil.getEntityManager();
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg = null;
        boolean loggedIn = false;
        String retorno = null;

        user.setSenha(encripta(user.getSenha()));
        Query q = em.createNamedQuery("UserSabatec.findUserPass")
                .setParameter("userSabatec", user.getUser())
                .setParameter("senhaSabatec", user.getSenha());
        this.users = q.getResultList();
        em.close();

        if (!users.isEmpty()) {
            loggedIn = true;
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Seja bem vindo!!", user.getUser());
            retorno =  "sucess";
        }else{
            
            loggedIn = false;  
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login", "Login invalido!!");
            retorno =  "nsucess";
        }
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("loggedIn", loggedIn);
        return retorno;
    }

    public void exclui(UserSabatec user) {

        EntityManager em = JpaUtil.getEntityManager();
        user = em.find(UserSabatec.class, user.getId());
        em.getTransaction().begin();
        em.remove(user);
        em.getTransaction().commit();
    }

    private static String encripta(String senha) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(senha.getBytes());
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(digest.digest());
        } catch (NoSuchAlgorithmException ns) {
            ns.printStackTrace();
            return senha;
        }
    }
}
